<template>
	<view class="container">
		<view class="form">
			<uv-form labelPosition="left" :model="model1" :rules="rules" ref="form" :errorType="errorType"
				:labelStyle="{'font-size':'28rpx'}">
				<uv-form-item label="营业执照照片" prop="company.image" labelWidth="100" borderBottom>
					<uv-upload :fileList="fileList1" name="1" multiple :maxCount="1" @afterRead="afterRead"
						@delete="deletePic"></uv-upload>
				</uv-form-item>
				<uv-form-item label="企业名称" prop="company.name" labelWidth="100" borderBottom>
					<uv-input v-model="model1.company.name" border="none" placeholder="请输入企业名称">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="企业信用代码" prop="company.code" labelWidth="100" borderBottom>
					<uv-input v-model="model1.company.code" border="none" placeholder="请输入企业信用代码" maxlength="18">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="法人姓名" prop="company.person" labelWidth="100" borderBottom>
					<uv-input v-model="model1.company.person" border="none" placeholder="请输入法人姓名">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="联系人" prop="company.contact" labelWidth="100" borderBottom>
					<uv-input v-model="model1.company.contact" border="none" placeholder="请输入联系人">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="电话" prop="company.phone" labelWidth="100">
					<uv-input v-model="model1.company.phone" border="none" placeholder="请输入电话" type="number"
						maxlength="11">
					</uv-input>
				</uv-form-item>
			</uv-form>
		</view>
		<view class="btn">
			<uv-button text="提交" color="#ff3d33" @click="submit" :customStyle="{width: '80%'}"></uv-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				// 错误提示
				errorType: 'toast',
				model1: {
					company: {
						image: '',
						name: '',
						code: '',
						person: '',
						contact: '',
						phone: '',
					},
				},
				rules: {
					'company.name': {
						type: 'string',
						required: true,
						message: '请填写企业名称',
						trigger: ['blur', 'change']
					},
					'company.code': {
						type: 'string',
						required: true,
						message: '请填写企业信用代码',
						trigger: ['blur', 'change']
					},
					'company.code': {
						type: 'string',
						len: 18,
						required: true,
						message: '企业信用代码格式不正确',
						trigger: ['blur', 'change']
					},
					'company.person': {
						type: 'string',
						required: true,
						message: '请填法人姓名',
						trigger: ['blur', 'change']
					},
					'company.contact': {
						type: 'string',
						required: true,
						message: '请输入联系人',
						trigger: ['blur', 'change']
					},
					'company.phone': {
						type: 'number',
						required: true,
						message: '请输入电话',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		onLoad() {
			this.getUserLicenseInfo();
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: '/wanlshop.common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let result = JSON.parse(res.data);
							this.model1.company.image = result.data.url;

							uni.request({
								url: '/wanlshop/user/getLicenseInfoFromImage',
								method: 'POST',
								data: {
									imageUrl: result.data.fullurl
								},
								success: (res) => {
									this.model1.company.name = res.res.data.companyName;
									this.model1.company.code = res.res.data.creditCode;
									this.model1.company.person = res.res.data.legalPerson;
								},
								complete: (res) => {
									this.$wanlshop.msg(res.res.msg);
								}
							});

							setTimeout(() => {
								resolve(result.data)
							}, 1000)
						}
					});
				})
			},
			// 提交
			submit() {
				uni.showLoading({
					title: "提交中..."
				})
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form.validate().then(res => {
					this.saveUserLicense();
				}).catch(errors => {
					this.$wanlshop.msg(errors[0].message);
				})
			},
			async getUserLicenseInfo() {
				uni.showLoading({
					title: "加载中..."
				})
				await uni.request({
					url: '/wanlshop.user/getUserLicenseInfo',
					method: 'POST',
					data: {},
					success: (res) => {
						if (res.res.code == 1) {
							let data = res.res.data;
							this.model1.company.name = data.company_name;
							this.model1.company.code = data.credit_code;
							this.model1.company.person = data.legal_person;
							this.model1.company.image = data.business_license;
							this.model1.company.contact = data.concat_name;
							this.model1.company.phone = data.concat_mobile;
							if (data.business_license) {
								this.fileList1.push({
									status: 'success',
									thumb: data.business_license,
									type: 'image'
								})
							}
						}
						uni.hideLoading();
						// this.$wanlshop.msg(res.res.msg);
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							title: '加载失败，请检查网络'
						});
					},
					complete: (res) => {
						uni.hideLoading();
						// this.$wanlshop.msg(res.msg);
					}
				});
			},
			async saveUserLicense() {
				await uni.request({
					url: '/wanlshop.user/saveUserLicense',
					method: 'POST',
					data: {
						company_name: this.model1.company.name,
						credit_code: this.model1.company.code,
						legal_person: this.model1.company.person,
						business_license: this.model1.company.image,
						concat_name: this.model1.company.contact,
						concat_mobile: this.model1.company.phone
					},
					success: (res) => {
						uni.hideLoading();
						this.$wanlshop.msg(res.res.msg);
						if (res.res.code == 1) {
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/user'
								});
							}, 2000)
						}
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							title: '加载失败，请检查网络'
						});
					},
					complete: (res) => {
						this.$wanlshop.msg(res.res.msg);
					}
				});
			},
		}
	}
</script>

<style>
	.uv-form-item {
		padding: 24rpx 26rpx;
	}

	.uv-button {
		width: 90%;
		margin: 0 auto;
	}
</style>

<style lang="scss" scoped>
	.container {
		margin: 32rpx;
	}

	.form {
		background: #FFFFFF;
		border-radius: 10rpx;
		padding: 20rpx 0rpx;
	}

	.btn {
		margin-top: 80rpx;
		width: 100%;
		display: flex;
		justify-content: center;
	}
</style>